package com.qc.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qc.sys.common.ActiveUser;
import com.qc.sys.common.Constant;
import com.qc.sys.common.DataGridView;
import com.qc.sys.common.ResultObj;
import com.qc.sys.entity.SysUser;
import com.qc.sys.service.SysUserService;
import com.qc.sys.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * (SysUser)表控制层
 *
 * @author AltmanQccc
 * @since 2020-08-26 11:38:08
 */
@RestController
@RequestMapping("user")
public class SysUserController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    /**
     * 查询用户
     * @param userVo
     * @return
     */
    @GetMapping("loadAllUser")
    public Object loadAllUser(UserVo userVo){
        return this.sysUserService.queryAllUser(userVo);
    }


    /**
     * 添加用户
     * */
    @PostMapping("addUser")
    public ResultObj addUser(SysUser user){
        try {
            this.sysUserService.InsertUser(user);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改用户
     * */
    @PostMapping("updateUser")
    public ResultObj updateUser(SysUser user){
        return this.sysUserService.UpdateUser(user);
    }

    /**
     * 删除用户
     * */
    @PostMapping("deleteUser")
    public ResultObj deleteUser(Integer id){
        try {
            this.sysUserService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 加载最大的排序码
     * */
    @GetMapping("queryUserMaxOrderNum")
    public Object queryUserMaxOrderNum(){
        Integer maxValue = this.sysUserService.queryUserMaxOrderNum();
        return new DataGridView(maxValue+1);
    }

    /**
     * 根据id查询用户信息
     * */
    @GetMapping("getUserById")
    public Object getUserById(Integer id){
        return this.sysUserService.getById(id);
    }

    /**
     * 根据id查询用户信息
     * */
    @GetMapping("resetUserPwd")
    public Object resetUserPwd(Integer id){
        return this.sysUserService.resetPwd(id);
    }

    /**
     * 保存用户和角色之间的关系
     * */
    @PostMapping("saveUserRole")
    public ResultObj saveUserRole(Integer uid,Integer[] rids){
        try {
            this.sysUserService.saveUserRole(uid,rids);
            return ResultObj.DISPATCH_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DISPATCH_ERROR;
        }
    }

    /**
     * 查询当前登录用户
     * */
    @GetMapping("getCurrentUser")
    public Object getCurrentUser(){
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        return new DataGridView(activeUser.getUser());
    }
}